package com.wk.code.wkserver.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wk.code.wkserver.entity.EstimateAmount;
import com.wk.code.wkserver.entity.TechType;
import com.wk.code.wkserver.mapper.EstimateAmountMapper;
import com.wk.code.wkserver.request.admin.EstimateAmountRequest;
import com.wk.code.wkserver.response.app.EstimateAmountResponse;
import com.wk.code.wkserver.response.base.PageInfo;
import com.wk.code.wkserver.service.IEstimateAmountService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author wq
 * @since 2021-08-18
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class EstimateAmountServiceImpl extends ServiceImpl<EstimateAmountMapper, EstimateAmount> implements IEstimateAmountService {

    @Override
    public void add(EstimateAmountRequest req) {
        if (StringUtils.isEmpty(req.getPrice())) {
            throw new RuntimeException("请输入价格区间");
        }
        EstimateAmount estimateAmount = new EstimateAmount();
        estimateAmount.setPrice(req.getPrice());
        save(estimateAmount);
    }

    @Override
    public void update(EstimateAmountRequest req) {
        EstimateAmount estimateAmount = new EstimateAmount();
        BeanUtils.copyProperties(req, estimateAmount);
        updateById(estimateAmount);
    }

    @Override
    public void delete(Integer id) {
        baseMapper.deleteById(id);
    }

    @Override
    public EstimateAmount detail(Integer id) {
        return getById(id);
    }

    @Override
    public PageInfo<EstimateAmount> page(EstimateAmountRequest req) {
        IPage<EstimateAmount> page = new Page<>(req.getPageNumber(), req.getPageSize());
        LambdaQueryWrapper<EstimateAmount> wrapper = new LambdaQueryWrapper<>();
        List<EstimateAmount> records = baseMapper.selectPage(page, wrapper).getRecords();
        if (!CollectionUtils.isEmpty(records)) {
            return new PageInfo<>(page.getTotal(), records);
        }
        return null;
    }

    @Override
    public List<EstimateAmountResponse> acquire() {
        return list().stream().map(x -> new EstimateAmountResponse().
                        setId(x.getId()).
                        setName(x.getPrice())).
                collect(Collectors.toList());
    }
}
